package org.jeecg.report.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.smallbun.screw.core.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.report.dto.EqpFaultHistoryDTO;
import org.jeecg.report.entity.EqpFaultHistory;
import org.jeecg.report.service.IEqpFaultHistoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.trace.dto.BarCodeTraceDTO;
import org.jeecg.trace.entity.QisReinspectValue;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 设备故障分类统计
 * @Author: LiCY
 * @Date:   2023-12-19
 * @Version: V1.0
 */
@Api(tags="设备故障分类统计")
@RestController
@RequestMapping("/report/eqpFaultHistory")
@Slf4j
public class EqpFaultHistoryController extends JeecgController<EqpFaultHistory, IEqpFaultHistoryService> {
	@Autowired
	private IEqpFaultHistoryService eqpFaultHistoryService;
	
	/**
	 * 分页列表查询666
	 *
	 * @param eqpFaultHistory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "设备故障分类统计-分页列表查询")
	@ApiOperation(value="设备故障分类统计-分页列表查询", notes="设备故障分类统计-分页列表查询")
	@GetMapping(value = "/list")
	@DS("#header.his")
	public Result<IPage<EqpFaultHistoryDTO>> queryPageList(EqpFaultHistory eqpFaultHistory,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<EqpFaultHistoryDTO> page = new Page<EqpFaultHistoryDTO>(pageNo, pageSize);
		IPage<EqpFaultHistoryDTO> pageList = eqpFaultHistoryService.pageList(page, eqpFaultHistory);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param eqpFaultHistory
	 * @return
	 */
	@AutoLog(value = "设备故障分类统计-添加")
	@ApiOperation(value="设备故障分类统计-添加", notes="设备故障分类统计-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:eqp_fault_history:add")
	@PostMapping(value = "/add")
	@DS("#header.line")
	public Result<String> add(@RequestBody EqpFaultHistory eqpFaultHistory) {
		eqpFaultHistoryService.save(eqpFaultHistory);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param eqpFaultHistory
	 * @return
	 */
	@AutoLog(value = "设备故障分类统计-编辑")
	@ApiOperation(value="设备故障分类统计-编辑", notes="设备故障分类统计-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:eqp_fault_history:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	@DS("#header.line")
	public Result<String> edit(@RequestBody EqpFaultHistory eqpFaultHistory) {
		eqpFaultHistoryService.updateById(eqpFaultHistory);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过选中对象删除
	 *
	 * @param eqpFaultHistory
	 * @return
	 */
	@AutoLog(value = "设备故障分类统计-通过id删除")
	@ApiOperation(value="设备故障分类统计-通过id删除", notes="设备故障分类统计-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:eqp_fault_history:delete")
	@RequestMapping(value = "/delete")
	public Result<String> delete(@RequestBody EqpFaultHistory eqpFaultHistory) {
		eqpFaultHistoryService.removeById(eqpFaultHistory);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param list
	 * @return
	 */
	@AutoLog(value = "设备故障分类统计-批量删除")
	@ApiOperation(value="设备故障分类统计-批量删除", notes="设备故障分类统计-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:eqp_fault_history:deleteBatch")
	@RequestMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestBody List<EqpFaultHistory> list) {
	    list.forEach(item->{
    			eqpFaultHistoryService.removeById(item);
    		});
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "设备故障分类统计-通过id查询")
	@ApiOperation(value="设备故障分类统计-通过id查询", notes="设备故障分类统计-通过id查询")
	@GetMapping(value = "/queryById")
	@DS("#header.his")
	public Result<EqpFaultHistory> queryById(@RequestParam(name="id",required=true) String id) {
		EqpFaultHistory eqpFaultHistory = eqpFaultHistoryService.getById(id);
		if(eqpFaultHistory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(eqpFaultHistory);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param eqpFaultHistory
    */
    //@RequiresPermissions("org.jeecg.modules.demo:eqp_fault_history:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, EqpFaultHistory eqpFaultHistory) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Page<EqpFaultHistoryDTO> page = new Page<EqpFaultHistoryDTO>(1, 9999);
		IPage<EqpFaultHistoryDTO> pageList = eqpFaultHistoryService.pageList(page, eqpFaultHistory);
		String title = "";
		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		//此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, EqpFaultHistoryDTO.class);
		//update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
		ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
		//update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
		mv.addObject(NormalExcelConstants.PARAMS, exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList.getRecords());
		return mv;
    }




    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("eqp_fault_history:importExcel")
    @DS("#header.line")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EqpFaultHistory.class);
    }

}
